// pages/commentView/commentView.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: true,
    commentData: [],
    BASE_URL: "http://127.0.0.1:9100/api",
    userId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const app = getApp();
    this.setData({
      userId: app.globalData.userId
    });
    this.getCommentList();
  },

  goBack() {
    wx.navigateBack({
      delta: 1
    });
  },

  handleClickTitle(e) {
    const item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: `/pages/thingDetail/thingDetail?id=${item.thingId}`
    });
  },

  getCommentList() {
    const { userId, BASE_URL } = this.data;
    const app = getApp();
    const token = app.globalData.token;
    wx.request({
      url: `${BASE_URL}/comment/listUserComments`,
      method: 'GET',
      header: {
        'TOKEN': token,
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: userId
      },
      success: (res) => {
        if (res.data.code === 200) {
          const commentData = res.data.data.map(item => {
            if (item.cover) {
              item.cover = `${BASE_URL}/staticfiles/image/${item.cover}`;
            }
            return item;
          });
          this.setData({
            commentData,
            loading: false
          });
        } else {
          wx.showToast({
            title: '获取评论列表失败',
            icon: 'none'
          });
          this.setData({
            loading: false
          });
        }
      },
      fail: (err) => {
        console.log(err.errMsg);
        wx.showToast({
          title: '请求失败',
          icon: 'none'
        });
        this.setData({
          loading: false
        });
      }
    });
  },
  
  formatTime(timestamp) {
    const date = new Date(parseInt(timestamp));
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hour = String(date.getHours()).padStart(2, '0');
    const minute = String(date.getMinutes()).padStart(2, '0');
    return `${year}-${month}-${day} ${hour}:${minute}`;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})